import akshare as ak
import pandas as pd
import time
import os
import json
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import cpu_count


allStocks = {}

def update_one(code):
    print('update_one %s' % (code))
    df = ak.stock_individual_info_em(symbol=code)
    item = {}
    for index, row in df.iterrows():
        item[row['item']] = row['value']

    allStocks[code] = item

def update():
    pool = ThreadPoolExecutor(max_workers=cpu_count())
    # 首先加载所有的股票代码，大概7000支
    df = ak.stock_zh_a_spot_em()

    print('get %d stocks' % (len(df)))
    print('cpu_count %d' % (cpu_count()))
    for index, row in df.iterrows():
        pool.submit(update_one, row['代码'])

    pool.shutdown(wait=True)

    with open("stock_info.json", 'w') as f:
        json.dump(allStocks, f, ensure_ascii=False)

    print('Done')

if __name__=='__main__':
    update()

